/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.service.CrudService;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.modules.zx.dao.LzdDao;
import com.zx.pm.modules.zx.dao.ProSupplierDao;
import com.zx.pm.modules.zx.dao.ProTimeNodeDao;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.ProTimeNode;

/**
 * 项目时间节点Service
 * @author forest
 * @version 2019-01-18
 */
@Service
@Transactional(readOnly = true)
public class ProTimeNodeService extends CrudService<ProTimeNodeDao, ProTimeNode> {

	@Autowired
	private LzdDao lzdDao;
	
	@Autowired
	private ProSupplierDao proSupplierDao;
	
	public ProTimeNode get(String id) {
		return super.get(id);
	}
	
	public List<ProTimeNode> findList(ProTimeNode proTimeNode) {
		return super.findList(proTimeNode);
	}
	
	public Page<ProTimeNode> findPage(Page<ProTimeNode> page, ProTimeNode proTimeNode) {
		return super.findPage(page, proTimeNode);
	}
	
	@Transactional(readOnly = false)
	public void save(ProTimeNode proTimeNode) {
		// 更新状态为项目时间节点管理阶段
		lzdDao.updateLzdStaut("3", proTimeNode.getLzdId());
		Lzd lzd = new Lzd();
		// 如果标段ID不为空，则证明是废弃的标段，标段ID为空，则证明废弃的是整个项目
		if(StringUtils.isNotBlank(proTimeNode.getSectionId())) {
			// 废弃标段
			if("0".equals(proTimeNode.getSection().getTrashPro())) {
				lzd.setTrashContent(proTimeNode.getSection().getTrashContent());
				lzd.setTrashPro(proTimeNode.getSection().getTrashPro());
				lzd.setSectionId(proTimeNode.getSectionId());
				lzd.setUpdateDate(new Date());
				lzdDao.updateSectionTrashPro(lzd);
			}
		}else {
			// 废弃项目
			if("0".equals(proTimeNode.getLzd().getTrashPro())) {
				lzd = proTimeNode.getLzd();
				lzd.setUpdateDate(new Date());
				lzdDao.updateTrashPro(lzd);
			}
			
		}
					
		super.save(proTimeNode);
	}
	
	@Transactional(readOnly = false)
	public void delete(ProTimeNode proTimeNode) {
		super.delete(proTimeNode);
	}
	
}